﻿using MediatR;
using U.B.DoMain;
using U.B.ErrorCode;
using U.B.ReadApi.Command;
using U.B.Repistory;

namespace U.B.ReadApi.CommandHander
{
    public class GetLeaderCommandHander : IRequestHandler<GetLeaderCommand, ApiResult<string>>
    {
        public readonly IRepistory<User> _userRepository;
        public GetLeaderCommandHander(IRepistory<User> userRepository)
        {
            _userRepository = userRepository;
        }
        public Task<ApiResult<string>> Handle(GetLeaderCommand request, CancellationToken cancellationToken)
        {
            ApiResult<string> result = new ApiResult<string>();
            result. code = Renum.显示成功;
            result.Message = "显示成功";
            //判断用户是领导/员工
            var u2 = _userRepository.GetAll().Where(x => x.UserId == request.UserId && x.PowerId == (int)PowerId.部门领导人).ToList();
            if (u2.Count() > 0)
            {
                var Names = _userRepository.GetAll().Where(x => x.PowerId == (int)PowerId.总负责人).Select(x => x.UserName).ToList();
                result.Data = string.Join(",", Names);
            }
            else
            {
                //否
                //通过部门Id和领导人查询到所有领导人的Ids
                var u = _userRepository.GetAll().Where(x => x.DepartmentId == request.DepId && x.PowerId == (int)PowerId.部门领导人).ToList();
                var LNames = u.Select(x => x.UserName).ToList();
                result.Data = string.Join(",", LNames);
            }
            return Task.FromResult(result);
        }
    }
}
